<?php
/**
 * Created by PhpStorm.
 * User: lingyun
 * Date: 2018/5/23
 * Time: 22:52
 * Desc: 商品配方
 */
namespace app\mall\controller;
use controller\BasicAdmin;
use service\DataService;
use think\Db;
use think\Request;

class Peifang extends BasicAdmin{
    /**
     * @Author  lingyun
     * @Desc    商品配方
     */
    public function index(){
        $id = input('id');      //商品id

        $this->title = '配方列表';
        $db = db('goods_spec_detail')->alias('a')->join('ys_goods b','a.goods_id=b.id','LEFT')->where(['a.is_deleted'=>0,'a.goods_id'=>$id])->field('a.*,b.name');
        $res = db('goods_spec_detail')->where(['goods_id'=>$id,'is_deleted'=>0])->field('id,peifang_ids')->select();
        $chengben = [];

        if(!empty($res)){
            foreach($res as $k=>$v){
                if(!empty($res[$k]['peifang_ids'])){
                    $sql = 'select sum(chengben) as chengben,sum(chengben_price) as chengben_price from ys_goods_peifang where id in ('.$v['peifang_ids'].')';
                    $jieguo = Db::query($sql);
                    $chengben[$v['id']]['chengben'] = $jieguo[0]['chengben'];
                    $chengben[$v['id']]['chengben_price'] = $jieguo[0]['chengben_price'];
                }else{
                    $chengben[$v['id']]['chengben'] = 0;
                    $chengben[$v['id']]['chengben_price'] = 0;
                }
            }
        }


        $this->assign('chengben',$chengben);
        $this->assign('title','商品配方');

//        $result = Db::query($sql);
        return parent::_list($db);
    }

    /**
     * @Author  lingyun
     * @Desc    分配配方
     * 2018-05-24
     */
    public function fenpei(){
        $request = request();
        if($request->isPost()){
            $post = input('post.');
            $data_arr['peifang_ids'] = implode(',',$post['authorize']);
            $data_arr['id'] = $post['id'];
            $chengben = db('goods_peifang')->where('id','in',$data_arr['peifang_ids'])->sum('chengben');
            $data_arr['goods_chengben'] = $chengben;
            $res = db('goods_spec_detail')->where('id',$post['id'])->update($data_arr);
            $res!==false?$this->success('保存成功',''):$this->error('保存失败，请稍后再试','');
        }else{
            $id = input('id');          //ys_goods_spec_detail  --- id
            $goods = db('goods_spec_detail')->alias('a')->join('ys_goods b','a.goods_id=b.id','LEFT')->field('a.id,a.goods_id,b.name,a.title,a.peifang_ids')->where(['a.id'=>$id])->find();

            $goods['peifang_ids'] = explode(',',$goods['peifang_ids']);
            $peifang = db('goods_peifang')->where(['is_deleted'=>0,'store_id'=>0])->select();

            $this->assign('peifang',$peifang);
            $this->assign('goods',$goods);
            return $this->fetch();
        }
    }

    /**
     * @Author  lingyun
     * @Desc    配方详情
     * 2018-05-24
     */
    public function detail(){
        $request = request();
        if(1){

        }else{
            $id = input('id');      //ys_goods_spec_detail --- id
            $peifang_ids = db('goods_spec_detail')->where('id',$id)->value('peifang_ids');
            $res = db('goods_peifang')->where('id','id',$peifang_ids)->select();


        }
    }

}